<template>
    <el-dialog :visible="visible" :title="title" @close="close" width="56%" :close-on-click-modal="false">
        <el-col :span="24">
            <el-col :span="11">
                <el-form>
                    <template v-for="(value,key,index) in listin">
                    <el-form-item
                    :label="value.identifier"
                    >
                    <el-input v-if="value.type==2" v-model="value.value1" :placeholder="'请输入'+value.identifier" clearable />
                    <el-input-number v-else v-model="value.value1" :placeholder="'请输入'+value.identifier"></el-input-number>
                    </el-form-item>
                    </template>
                </el-form>
            </el-col>
            <el-col :span="2"></el-col>
            <el-col :span="11">
                <pre>{{data}}</pre>
            </el-col>
        </el-col>
        <div class="clear"></div> 
    </el-dialog>
</template>
<script>
export default {
    name: 'cmdShow',
    props: {
      visible: {
      type: Boolean,
      required: true,
    },
      listin: {
          type: Array,
          required: true,
      },
      cmdId: {
        type: Number,
        required: true,
      },
      isDown: {
        type: Boolean,
        required: true,
      },
    },
    data () {
        return {
            data1: {"msgId": 1,"action": "devSend","params": {"devTid": "ESP_2M_245EC89","appTid": []}},
            data2: {"msgId": 1,"action": "appSend","params": {"devTid": "ESP_2M_245EC89","ctrlKey": "01335a277ffc3c5fc9ba40df63a3e25c","appTid": []}},
        }
    },
    computed: {
        data () {
            var updata = Object.assign({},this.isDown==true?this.data2:this.data1);
            var de = {};
            de['ca1_@@cmd']=this.cmdId;
            for(var i=0;i<this.listin.length;i++){
               var name = "ca" +(i+2) +"_@@" + this.listin[i].identifier;
               de[name] = this.listin[i].value1;
            }

            updata.params.data = de;
            var stringinit = JSON.stringify(updata,null,2);
            var stringa = stringinit.replaceAll(/(ca[1-9]\d*_@@)/g,'');
            return stringa
        },
        title () {
            return  '查看'+ (this.isDown==true?'下发帧':'上报帧')+'命令示例'
        } 
    },
    methods: {
        close() {
            this.$emit('update:visible', false);
        },
    }
}
</script>